﻿using MongoDB.Bson;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Gear.NoSql.Core.Tool
{
    public static class NoSqlTool
    {
        /// <summary>
        /// String to BsonDocument
        /// </summary>
        /// <param name="json"></param>
        /// <returns></returns>
        public static BsonDocument StrToBD(string json) => MongoDB.Bson.Serialization.BsonSerializer.Deserialize<BsonDocument>(json);

        /// <summary>
        /// Guid
        /// </summary>
        public static string EntityID
        {
            get
            {
                return Guid.NewGuid().ToString().Replace("-", "").ToLower();
            }
        }

        /// <summary>
        /// 二进制流转图片
        /// </summary>
        /// <param name="imgBytesIn"></param>
        /// <returns></returns>
        public static Bitmap ByteToImg(byte[] imgBytesIn)
        {
            MemoryStream ms = new MemoryStream(imgBytesIn);
            Bitmap bmp = new Bitmap(ms);
            ms.Close();
            return bmp;
        }

        /// <summary>
        /// 图片转base64
        /// </summary>
        /// <param name="bmp"></param>
        /// <returns></returns>
        public static string ImgToBase64(Bitmap bmp) => Convert.ToBase64String(ImgToByte(bmp));
        /// <summary>
        /// 图片转byte
        /// </summary>
        /// <param name="bmp"></param>
        /// <returns></returns>
        public static byte[] ImgToByte(Bitmap bmp)
        {
            MemoryStream ms = new MemoryStream();
            bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
            byte[] bytes = new byte[ms.Length];
            ms.Position = 0;
            ms.Read(bytes, 0, (int)ms.Length);
            ms.Close();

            return bytes;
        }

        /// <summary>
        /// 解析JSON数组生成对象实体集合
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="json"></param>
        /// <returns></returns>
        public static List<T> DeserializeJsonToList<T>(string json) where T : class
        {
            JsonSerializer serializer = new JsonSerializer();
            StringReader sr = new StringReader(json);
            object o = serializer.Deserialize(new JsonTextReader(sr), typeof(List<T>));
            List<T> list = o as List<T>;
            return list;
        }
        /// <summary>
        /// JsonStrToJObject
        /// </summary>
        /// <param name="jsonStr"></param>
        /// <returns></returns>
        public static JObject JsonStrToJObject(string jsonStr) => (JObject)JsonConvert.DeserializeObject(jsonStr);
        /// <summary>
        /// JsonStrToJArray
        /// </summary>
        /// <param name="jsonStr"></param>
        /// <returns></returns>
        public static JArray JsonStrToJArray(string jsonStr) => (JArray)JsonConvert.DeserializeObject(jsonStr);
        /// <summary>
        /// JArrayToList
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="array"></param>
        /// <returns></returns>
        public static List<T> JArrayToList<T>(JArray array) where T : class => array.ToObject<List<T>>();
        /// <summary>
        /// StreamToByte
        /// </summary>
        /// <param name="stream"></param>
        /// <returns></returns>
        public static byte[] StreamToByte(Stream stream)
        {
            byte[] buffer = new byte[stream.Length];
            stream.Read(buffer, 0, buffer.Length);
            return buffer;
        }
        /// <summary>
        /// ByteToStream
        /// </summary>
        /// <param name="buffer"></param>
        /// <returns></returns>
        public static Stream ByteToStream(byte[] buffer)
        {
            Stream stream = new MemoryStream(buffer);
            stream.Seek(0, SeekOrigin.Begin);
            //设置stream的position为流的开始
            return stream;
        }
        /// <summary>
        /// 提取文件后缀
        /// </summary>
        /// <param name="filename"></param>
        /// <returns></returns>
        public static string GetSuffix(string filename) => filename.Split('.')[1];
        /// <summary>
        /// 提取文件名
        /// </summary>
        /// <param name="filename"></param>
        /// <returns></returns>
        public static string GetAntonym(string filename) => filename.Split('.')[0];
        /// <summary>
        /// 从一个对象信息生成Json串
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string ObjectToJson(object obj) => JsonConvert.SerializeObject(obj);
        /// <summary>
        /// 从一个Json串生成对象信息
        /// </summary>
        /// <param name="jsonString"></param>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static object JsonToObject(string jsonString, object obj) => JsonConvert.DeserializeObject(jsonString, obj.GetType());
        /// <summary>
        /// 解析JSON字符串生成对象实体
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="json"></param>
        /// <returns></returns>
        public static T DeserializeJsonToObject<T>(string json) where T : class
        {
            try
            {
                JsonSerializer serializer = new JsonSerializer();
                StringReader sr = new StringReader(json);
                object o = serializer.Deserialize(new JsonTextReader(sr), typeof(T));

                T t = o as T;
                return t;
            }
            catch (Exception)
            {
                return null;
            }
        }
    }
}
